//
//  TitleViewManager.m
//  OrderOnline
//
//  Created by YAPPA_3 on 11/28/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "TitleViewManager.h"
#import "DataManager.h"
#import "OrderOnlineViewController.h"
#import "DMMenuFood.h"
enum
{	
	eStepXmlRequest = 0,
	eStepXmlWait,	
	eStepGotoNext,
};


@implementation TitleViewManager


- (id)initWithFrame:(CGRect)frame {
    
    if( self == [super initWithFrame:frame] )
	{
		CGFloat oy = 0; 	
		NSString* bg_path = nil;	
		if ( UIDeviceOrientationIsLandscape([[OrderOnlineViewController Shared] m_DeviceOrie]) ) {
			bg_path = [[NSBundle mainBundle] pathForResource:@"Default-Landscape" ofType:@"png"];
		} else {
			bg_path = [[NSBundle mainBundle] pathForResource:@"Default-Portrait" ofType:@"png"];
		}				
		if ( bg_path == nil ) {
			bg_path = [[NSBundle mainBundle] pathForResource:@"Default_iPad" ofType:@"png"];
		}
		oy = 10;		 	
		m_Background = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:bg_path]];
		m_Background.center = CGPointMake(frame.size.width/2, frame.size.height/2 + oy);
				
		[self addSubview:m_Background];		
		m_Step = eStepXmlRequest;
	}
	return self;
}
#pragma mark -
#pragma mark ViewBase protcols

- (void)update
{	
   	switch( m_Step )
	{					
		case eStepXmlRequest:
		{			
			NSData* data = [[DataManager Shared] loadFromResource:MENU_FOOD];		
			DMMenuFood* unit = [[DataManager Shared] getDMUnit:eDmtMenuFoodXml];
			if ( data != nil )
			{							
				if ([unit load:MENU_FOOD]) {
					m_Step = eStepGotoNext;
				}
			}
			else {
				[unit requestFileXML];				
				m_Step = eStepGotoNext;
			}

			
		} break;
			
			
		case eStepXmlWait: 
		{
		} break;
									
		case eStepGotoNext:
		{
			
			[[OrderOnlineViewController Shared] changeHomeView:YES];
		} break;
			
		default: break;
	}
}


#pragma mark -
#pragma mark Downloader delegates

- (void)downloaderError:(NSString*)url_path
{
		DebLog(@"DLErr");
}

- (void)downloaderCancel
{
	DebLog(@"DLCancel");
}

- (void)downloaderFinish:(NSData*)data saveName:(NSString*)save_name
{
	
}


- (void)dealloc
{
	SAFE_RELEASE(m_Background);
	SAFE_RELEASE(m_AlertEnd);
	SAFE_RELEASE(m_AlertPass);
	SAFE_RELEASE(m_RequestAPI)
	
    [super dealloc];
}


@end
